<?php

namespace App\Ma\Entities;

class Card extends Matrix
{


    /**
     * 碰
     * @var array
     */
    public $pengList = [];


    /**
     * 暗杠
     * @var array
     */
    public $anGangList = [];

    /**
     * 明杠
     * @var array
     */
    public $mGangList = [];






    /**
     * 杠牌
     * @param int $value
     * @param bool $isAnGang
     * @return Card|bool
     */
    public function gang(int $value, bool $isAnGang = false)
    {
        if (!$this->canGang($value)) {
            return false;
        }
        $type = MatrixUtils::getValueIndex($value, 2);
        $val = MatrixUtils::getValueIndex($value, 3);
        $this->cardMatrix[$type][$val] -= 2;
        $count = 0;
        $_cards = $this->cards;
        $cards = [];
        foreach ($_cards as $key => $card) {
            if ($count < 3 && $value === $card) {
                $count++;
                continue;
            }
            $cards[] = $card;
        }
        $this->cards = $cards;
        $this->twoCardsCounts();
        // 杠后无法实现七对胡牌
        $this->canUseLaiZiCount = 14;
        $this->canUseLaiZiKangCount = 14;
        if ($isAnGang) {
            array_push($this->mGangList, $value);
        } else {
            array_push($this->anGangList, $value);
        }

        return $this;
    }

    /**
     * 碰牌
     * @param int $value
     * @return Card|bool
     */
    public function peng(int $value)
    {
        if (!$this->canPeng($value)) {
            return false;
        }
        $type = MatrixUtils::getValueIndex($value, 2);
        $val = MatrixUtils::getValueIndex($value, 3);
        $this->cardMatrix[$type][$val] -= 2;
        $count = 0;
        $_cards = $this->cards;
        $cards = [];
        foreach ($_cards as $key => $card) {
            if ($count < 2 && $value === $card) {
                $count++;
                continue;
            }
            $cards[] = $card;
        }
        $this->cards = $cards;
        // 碰后无法实现七对胡牌
        $this->canUseLaiZiCount = 14;
        $this->canUseLaiZiKangCount = 14;
        array_push($this->pengList, $value);
        return $this;
    }

    



}